{extend name='public/content'}

{block name="title"}
	商品详情
{/block}

{block name="body"}
	{if  condition="($list == !null)"}
		<div class="layui-container">  
			<div class="layui-row">
			    <div class="layui-col-md6">
			     	{$list.name}
			    </div>
			    <div class="layui-col-md6">
			      <img src="{$list.logo}" style="width: 100px;height: 100px">
			    </div>
			</div>
		</div>
		<div>库存：<span class="kucun">{$stock}</span></div>
		{$list.description}

		<div></div>
		{foreach($list.commt as $k=>$v)}
			名称{$v.shifonimin}
			内容{$v.content}
			时间{$v.addtime}
			使用：购买价格
			</br>
		{/foreach}
	<div class="price"></div>
		{foreach($goodspace as $kk=>$vo)}
		<div>
			{$vo['spec_id']}
			<input type="hidden" id="spec_id{$vo['spec_id']}" class="shuxinxuanze" name="spec_id{$vo['spec_id']}" data-shuxin="{$vo['spec_name']}" value=''>
			{$vo['spec_name']}</br>
				{foreach($vo['content'] as $key => $vo)}
					id值  {$vo['id']}
					价格  {$vo['price']}
				    <button class="dataoption" data-inputid="spec_id{$vo['spec_id']}" data-option="{$vo['id']}">选择</button>
				{/foreach}
			</br>
		</div>
		{/foreach} 
		<div class="sumclick layui-btn" data-type="-">-</div><input type="num" name="sum" value="1"><div class="sumclick layui-btn" data-type="+">+</div>
		<input type="hidden" name="storid" value="{$storeid}">
		<button id="colegoodid" data-id='{$list.id}'>加入收藏</button>
		<button id="addusecar" data-id='{$list.id}'>加入购物车</button>
		<button id="orderprice" data-id='{$list.id}'>立即购买</button>
		<div><?php echo htmlspecialchars_decode($list['detail']); ?></div>
	<style type="text/css">
		.red{
			color: red;
		}
	</style>
		
{else/}
	该商品已下架
{/if}
{/block}

{block name="script"}
 	<script type="text/javascript">
 		{if  condition="($list == !null)"}
 		var url={
 			user : "{:url('index/user/index')}",
 			collegood:  "{:url('index/order/collegood',['id'=>$list['id']])}",
 			addusecar : "{:url('index/order/addusecar',['id'=>$list['id']])}",
 			orderpre :     "{:url('index/order/preindex')}",
 			goodproajax :     "{:url('index/index/getgoodsajax')}"
 		}
 		//最后的商品规格
 		var specid = '-1';
 		require(['jquery'], function () {
 			$('.sumclick').click(function(){
 				var that = $(this);
 				var num = parseInt($('input[name=sum]').val());
 				if(that.data("type")=="-"){
 					if(num<0 || num==0){
 						$('input[name=sum]').val(0)
 					}else{
 						$('input[name=sum]').val(num-1)
 					}
 				}
 				if(that.data("type")=="+"){

 					$('input[name=sum]').val(num+1)
 				}
 			})
 		 	//商品收藏
 		 	$('#colegoodid').click(function(){
 		 		
 		 		$.ajax({
 		 			url: url.collegood,
 		 			type: 'GET',
 		 			dataType: 'json',
 		 			data: {}
 		 		})
 		 		.done(function($result) {
 		 			if($result.status=='9'){
 		 				window.location.href=url.user;
 		 			}
 		 			if($result.status=='5'){
 		 				alert('取消收藏');
 		 			}
 		 			if($result.status=='1'){
 		 				alert('收藏成功');
 		 			}
 		 		})
 		 		.fail(function() {
 		 			console.log("error");
 		 		})
 		 		.always(function() {
 		 			console.log("complete");
 		 		});
 		 	})

 		    //选择 
 		    
 		    $('.dataoption').click(function(){
 		    	var inpuid = $(this).data('inputid');
 		    	var optionid = $(this).data('option');
 		    	//alert(inpuid);alert(optionid);
 		    	$('#'+inpuid).val(optionid);

 		    	$(this).siblings().each(function(index, el) {
 		    		if($(el).hasClass("red")){
 		    			$(el).removeClass('red')
 		    		}
 		    	}); 
 		    	$(this).addClass('red') ;
 		    	let shuxin=[];
 		    	$('.shuxinxuanze').each(function(index, elem) {
 		 			if($(elem).val()!=''){
 		 				shuxin.push($(elem).val());
 		 			}
 		 		}) 
 		 		let data =shuxin.join(',',shuxin);
 		 		console.dir(data);
 		 		$.ajax({
 		 			url: url.goodproajax,
 		 			type: 'GET',
 		 			dataType: 'json',
 		 			data: {param: data},
 		 		})
 		 		.done(function(data) {
 		 			if(data.length==1){
 		 				$('.kucun').text(data[0].stock);
 		 			}

 		 			if(data=='1'){
 		 				alert("没有中商品");
 		 			}
 		 			specid = data[0].id;
 		 			if(data.kuconu==0){
 		 				alert("duibuqi meicunnj");
 		 			}
 		 		    $('.price').text(data[0].price);
 		 		
 		 		})
 		 		.fail(function() {
 		 			//console.log("error");
 		 		})
 		 		.always(function() {
 		 			//console.log("complete");
 		 		});
 		 		
 		    })
 		 	//加入购物车
 		 	$('#addusecar').click(function(){
 		 		var storeid =$('input[name=storid]').val();
 		 		var guige =$('.dataoption');
 		 		var shuxin =[];
 		 		var dis =0;
 		 		$('.shuxinxuanze').each(function(index, elem) {

 		 			if($(elem).val()==null || $(elem).val()==''){
 		 				alert("请选择"+$(elem).data('shuxin'));
 		 				//console.dir(1);
						//dis =1;
 		 				return false ;
 		 			}
 		 			shuxin.push($(elem).val());
 		 		}) 
 		 		var sum = parseInt($('input[name=sum]').val());
 		 		$.ajax({
 		 			url: url.addusecar,
 		 			type: 'GET',
 		 			dataType: 'json',
 		 			data: {sum:sum,storeid:storeid,spaceid:specid},
 		 		})
 		 		.done(function($result) {
 		 			if($result.status=='9'){
 		 				window.location.href=url.user;
 		 			}	
 		 		})
 		 		.fail(function() {
 		 			console.log("error");
 		 		})
 		 		.always(function() {
 		 			console.log("complete");
 		 		});
 		 	})

 		 	/**
 		 	 * 立即购买
 		 	 */
 		 	$('#orderprice').click(function(event) {
 		 		var sum = parseInt($('input[name=sum]').val());
 		 		var num = $('.kucun').text();
 		 		if(parseInt(num)<sum){
 		 			alert("库存不够");
 		 			return;
 		 		}
 		 		var storeid =$('input[name=storid]').val();
 		 		var guige =$('.dataoption');
 		 		var shuxin =[];
 		 		var	dis = 0;
 		 		$('.shuxinxuanze').each(function(index, elem) {
 		 			if($(elem).val()==null || $(elem).val()==''){
 		 				alert("请选择"+$(elem).data('shuxin'));
						dis 	=	1;
 		 				return false ;
 		 			}
 		 			shuxin.push($(elem).val());
 		 		})
 		 		if(dis==1){
 		 			return;
 		 		}
 		 		
 		 		var	id =$(this).data('id');
 		 		
 		 		window.location.href=url.orderpre+'?id='+id+'&sum='+sum+'&storeid='+storeid+'&spaceid='+specid;
 		 	});
 		 })
 		{/if}
 	</script>
{/block}

{block name="style"}
 <style type="text/css">
 	
 </style>
{/block}